// Copyright (c) 2022 Presto Labs Pte. Ltd.
// Author: xguo

#include "coin2/exchange/mexc_futures/api_order/native_rest_async_asio_subscriber.h"

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>

#include "coin2/base/log.h"

namespace coin2::exchange::mexc_futures::api_order {

void MexcFuturesRestAsyncAsioSubscriber::QueryBalance() {
  auto http_context = rest_client()->QueryAccountBalance();
  if (!http_context) return;
  http_context->handler =
      std::bind(&MexcFuturesRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void MexcFuturesRestAsyncAsioSubscriber::QueryPosition() {
  auto http_context = rest_client()->QueryAccountPosition();
  if (!http_context) return;
  http_context->handler =
      std::bind(&MexcFuturesRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void MexcFuturesRestAsyncAsioSubscriber::QueryOpenOrders() {
  auto http_context = rest_client()->QueryOpenOrders();
  if (!http_context) return;
  http_context->handler =
      std::bind(&MexcFuturesRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void MexcFuturesRestAsyncAsioSubscriber::QueryHistoryOrders() {
  auto http_context = rest_client()->QueryAllOrders();
  if (!http_context) return;
  http_context->handler =
      std::bind(&MexcFuturesRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void MexcFuturesRestAsyncAsioSubscriber::QueryFills(const std::string& native_symbol) {
  auto http_context = rest_client()->QueryTrades(native_symbol);
  if (!http_context) return;
  http_context->handler =
      std::bind(&MexcFuturesRestAsyncAsioSubscriber::HandleQuery, this, std::placeholders::_1);
  http_driver_->PostAsyncContext(http_context);
}

void MexcFuturesRestAsyncAsioSubscriber::SubscribeAsync() {
  AddPeriodicTask(
      std::bind(&MexcFuturesRestAsyncAsioSubscriber::QueryBalance, this),
      config_.query_account_balance_period_sec(),
      "query_balance");

  AddPeriodicTask(
      std::bind(&MexcFuturesRestAsyncAsioSubscriber::QueryPosition, this),
      config_.query_account_position_period_sec(),
      "query_position");

  /*
  auto symbol_list = native_symbols();

  AddPeriodicTask(
      std::bind(&MexcFuturesRestAsyncAsioSubscriber::QueryOpenOrders, this),
      config_.query_open_orders_period_sec(),
      "query_open_orders");

  AddPeriodicTask(
      std::bind(&MexcFuturesRestAsyncAsioSubscriber::QueryHistoryOrders, this),
      config_.query_cancel_orders_period_sec(),
      "query_history_orders");

  AddPeriodicTask(
      std::bind(&MexcFuturesRestAsyncAsioSubscriber::QueryFills, this, std::placeholders::_1),
      config_.query_fill_period_sec(),
      "query_fills",
      symbol_list);
  */
}

}  // namespace coin2::exchange::mexc_futures::api_order
